package com.animatedpicture.panel;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

import com.animatedpicture.object.IMovingObject;

public class DrawPanel extends JPanel {

	private static ArrayList<IMovingObject> movingObjs = new ArrayList<>();

	// Background
	private ImageIcon bg;

	private static AudioStream as;
	
	public DrawPanel()
	{
		setPreferredSize(new Dimension(200, 200));

		// Get Background Image
		bg = new ImageIcon("background.jpg");

		addMouseListener(new PanelMouseListener());
		
		// Audio
		try {
			File file = new File("./sound/Time is running out - Muse.wav");
			FileInputStream fis = new FileInputStream(file);
			as = new AudioStream(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Add item to list.
	 * @param object IMovingObject type object.
	 */
	public void addItem(IMovingObject object)
	{
		movingObjs.add(object);

	}

	/**
	 * Start animation.
	 */
	public void animateItem()
	{
		// When the number of drawed item is 1, then play object's own music.
		if (movingObjs.size() == 1) {
			movingObjs.get(movingObjs.size()-1).playMusic(true);
		}else {
			playMusic(true);
		}
		
		for (IMovingObject element : movingObjs)
		{
			element.animate(true);
		}
	}

	/**
	 * Stop animation effect.
	 */
	public void stopAnimate()
	{
		playMusic(false);
		for (IMovingObject element : movingObjs)
		{
			element.animate(false);
			element.playMusic(false);
		}
	}

	/**
	 * Clear whole objects.
	 */
	public void clear()
	{
		movingObjs.clear();
	}
	
	/**
	 * Play or stop music
	 * @param trriger If the value is true, play. But the value is false, stop playing music.
	 */
	private void playMusic(boolean trriger)
	{
		if (trriger) {
			AudioPlayer.player.start(as);
		}else {
			AudioPlayer.player.stop(as);
		}
	}

	/**
	 * Set Panel's background image.
	 * @param mFile The background image file.
	 */
	public void setBackground(File mFile)
	{
		try {
			BufferedImage bufferImg = ImageIO.read(mFile);
			bg = new ImageIcon(bufferImg);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * Set background music.
	 * @param fileName The name of wav file.
	 */
	public void setBGM(String fileName)
	{
		try {
			File file = new File("./sound/"+fileName+".wav");
			System.out.println("File name : "+file.getName());
			FileInputStream fis = new FileInputStream(file);
			as = new AudioStream(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void paint(Graphics g) {
 
		// Draw Background
		g.drawImage(bg.getImage(), 0, 0, this.getWidth(), this.getHeight(), null);

		for (IMovingObject element : movingObjs)
		{
			Graphics2D g2 = (Graphics2D)g;
			element.draw(g2);
		}

	}

	// Mouse Listener
	// Get mouse position when mouse pressed
	class PanelMouseListener implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent e) {
			if (movingObjs.size() != 0) {
				movingObjs.get(movingObjs.size() - 1).setPosition(e.getPoint());
			}else {
				JOptionPane.showMessageDialog(getParent(), "Select item first", "Error", JOptionPane.ERROR_MESSAGE, null);
			}
		}

		@Override
		public void mousePressed(MouseEvent e) {

		}

		@Override
		public void mouseReleased(MouseEvent e) {

		}

		@Override
		public void mouseEntered(MouseEvent e) {
			
		}

		@Override
		public void mouseExited(MouseEvent e) {

		}

	}
 }
